package com.zth.inter;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginIntegr implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//到达controller之前  判断一下session里面 是否有值  有 true
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        System.out.println("user = " + user);
        if (user == null) {
            //项目名字/view/login.jsp
            //跳转到登录页面  重定向
            String contextPath = request.getContextPath();//上下文路径
            String path = contextPath + "succes.jsp";
            System.out.println("path = " + path);
            response.sendRedirect(path);
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
